package com.ys.service.bi.impl;


import com.alibaba.fastjson.JSONObject;
import com.itextpdf.text.log.Logger;
import com.itextpdf.text.log.LoggerFactory;
import com.ys.entity.bi.AccountCountForCompanyHistoryBi;
import com.ys.entity.bi.BiBusinessFundsRemark;
import com.ys.entity.finance.TcwAccountCountForCompany;
import com.ys.entity.hr.ThrOperateCompanyApply;
import com.ys.mapper.bi.BiBusinessFundsRemarkMapper;
import com.ys.mapper.bi.BiMapper;
import com.ys.service.bi.BiBusinessFundsService;
import com.ys.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.*;
import java.util.stream.Collectors;

/**
 * Bi业务资金
 */
@Service("biBusinessFundsService")
public class BiBusinessFundsServiceImpl implements BiBusinessFundsService {
	private static final Logger logger = LoggerFactory.getLogger(BiBusinessFundsServiceImpl.class);


	@Autowired
	private BiMapper biMapper;

	@Autowired
	private BiBusinessFundsRemarkMapper biBusinessFundsRemarkMapper;


	/**
	 * 获取Bi业务资金逻辑
	 * @return
	 */
	@Override
	public JSONObject getBusinessFunds() {
		JSONObject res = new JSONObject();
		//标题
		List<String> title = new ArrayList<>();
		//获取近七天的标题
		LocalDate today = LocalDate.now();
		for (int i = 1; i < 8; i++) {
			LocalDate lastDay = today.minusDays(i);
			title.add(lastDay.toString());
		}
		res.put("title",title);

		//最终数据
		List<Map<String, Object>> dataList = new ArrayList<>();
		Map<String, Object> data;
		//公司账户
		BigDecimal companyAccountDje = BigDecimal.ZERO;
		//直营账户
		BigDecimal directAccountDje = BigDecimal.ZERO;
		//过渡账户
		BigDecimal transitionAccountDje = BigDecimal.ZERO;
		//运营账户
		BigDecimal operatingAccountDje = BigDecimal.ZERO;
		//停用账户
		BigDecimal deactivateAccountDje = BigDecimal.ZERO;
		//其它账户
		BigDecimal otherAccountsDje = BigDecimal.ZERO;
		//查询7天内的历史资金数据
		List<AccountCountForCompanyHistoryBi> accountCountForCompanyHistorySeven = biMapper.getAccountCountForCompanyHistorySeven();
		List<AccountCountForCompanyHistoryBi> history1;
		List<AccountCountForCompanyHistoryBi> history2;
		List<AccountCountForCompanyHistoryBi> history3;
		List<AccountCountForCompanyHistoryBi> history4;
		List<AccountCountForCompanyHistoryBi> history5;
		List<AccountCountForCompanyHistoryBi> history6;
		List<AccountCountForCompanyHistoryBi> history7;

		if (accountCountForCompanyHistorySeven.size() == 0){
			return res;
		}
		//查询账户信息
		List<Map<String, Object>> operateCompanyApplyList = new ThrOperateCompanyApply()
				.addLeftJoin("TCW_AccountCountForCompany B ON B.vgsBm = A.vgsBm")
				.addField("B.fJe,B.vgsJc,")
				.queryMap();
		for (Map<String, Object> tcwAccountCountForCompanyMap : operateCompanyApplyList){
			//前一天数据
			history1 = accountCountForCompanyHistorySeven.stream().filter(
					item -> item.getVgsBm().equals(tcwAccountCountForCompanyMap.get("vgsbm")) &&
							item.getGdate().equals(title.get(0))
			).collect(Collectors.toList());
			//是否停用
			if (new Integer(tcwAccountCountForCompanyMap.get("deleted").toString()) == 0) {
				if (tcwAccountCountForCompanyMap.get("operationsAttribute").toString() != null) {
					if (tcwAccountCountForCompanyMap.get("operationsAttribute").toString().equals("1") || tcwAccountCountForCompanyMap.get("operationsAttribute").toString().equals("4")) {
						data = new HashMap<>();
						data.put("vgsBm", tcwAccountCountForCompanyMap.get("vgsbm"));
						data.put("vgsJc", tcwAccountCountForCompanyMap.get("vgsJc"));
						data.put("vzhlx", tcwAccountCountForCompanyMap.get("operationsAttribute").toString().equals("1") ? "公司账户" : "直营账户");

						//前2天
						history2 = accountCountForCompanyHistorySeven.stream().filter(
								item -> item.getVgsBm().equals(tcwAccountCountForCompanyMap.get("vgsbm")) &&
										item.getGdate().equals(title.get(1))
						).collect(Collectors.toList());
						//前3天
						history3 = accountCountForCompanyHistorySeven.stream().filter(
								item -> item.getVgsBm().equals(tcwAccountCountForCompanyMap.get("vgsbm")) &&
										item.getGdate().equals(title.get(2))
						).collect(Collectors.toList());
						//前3天
						history4 = accountCountForCompanyHistorySeven.stream().filter(
								item -> item.getVgsBm().equals(tcwAccountCountForCompanyMap.get("vgsbm")) &&
										item.getGdate().equals(title.get(3))
						).collect(Collectors.toList());
						//前4天
						history5 = accountCountForCompanyHistorySeven.stream().filter(
								item -> item.getVgsBm().equals(tcwAccountCountForCompanyMap.get("vgsbm")) &&
										item.getGdate().equals(title.get(4))
						).collect(Collectors.toList());
						//前5天
						history6 = accountCountForCompanyHistorySeven.stream().filter(
								item -> item.getVgsBm().equals(tcwAccountCountForCompanyMap.get("vgsbm")) &&
										item.getGdate().equals(title.get(5))
						).collect(Collectors.toList());
						//前6天
						history7 = accountCountForCompanyHistorySeven.stream().filter(
								item -> item.getVgsBm().equals(tcwAccountCountForCompanyMap.get("vgsbm")) &&
										item.getGdate().equals(title.get(6))
						).collect(Collectors.toList());

						data.put("changes", history2.get(0).getfJe().subtract(history1.get(0).getfJe()));
						data.put("history1", history1.get(0).getfJe());
						data.put("history2", history2.get(0).getfJe());
						data.put("history3", history3.get(0).getfJe());
						data.put("history4", history4.get(0).getfJe());
						data.put("history5", history5.get(0).getfJe());
						data.put("history6", history6.get(0).getfJe());
						data.put("history7", history7.get(0).getfJe());
						data.put("remark", history1.get(0).getRemark());
						dataList.add(data);
//
						if (tcwAccountCountForCompanyMap.get("operationsAttribute").toString().equals("1")) {
							companyAccountDje = companyAccountDje.add(history1.get(0).getfJe());
						} else {
							directAccountDje = directAccountDje.add(history1.get(0).getfJe());
						}
					} else if (tcwAccountCountForCompanyMap.get("operationsAttribute").toString().equals("3")) {
						//过渡账户
						transitionAccountDje = transitionAccountDje.add(history1.get(0).getfJe());
						//运营账户
					} else if (tcwAccountCountForCompanyMap.get("operationsAttribute").toString().equals("2")) {
						operatingAccountDje = operatingAccountDje.add(history1.get(0).getfJe());
					}
//
					//未标记运营属性的账户+未停用账户
					if (!tcwAccountCountForCompanyMap.get("operationsAttribute").toString().equals("1")) {
						otherAccountsDje = otherAccountsDje.add(history1.get(0).getfJe());
					}
				}
			}else {
				//停用账户
				deactivateAccountDje = deactivateAccountDje.add(history1.get(0).getfJe());
			}
		}
		

		//排序按照昨日资金
		Collections.sort(dataList, new Comparator<Map<String, Object>>(){

			public int compare(Map<String, Object> o1, Map<String, Object> o2) {
				BigDecimal changes1 =new BigDecimal(o1.get("changes").toString());
				BigDecimal changes2= new BigDecimal(o2.get("changes").toString());
				return changes2.compareTo(changes1);
			}

		});

		res.put("dataList",dataList);
		res.put("companyAccountDje",companyAccountDje);
		res.put("directAccountDje",directAccountDje);
		res.put("transitionAccountDje",transitionAccountDje);
		res.put("operatingAccountDje",operatingAccountDje);
		res.put("deactivateAccountDje",deactivateAccountDje);
		res.put("otherAccountsDje",otherAccountsDje);
		String updateTime1 = "未知";
		if (accountCountForCompanyHistorySeven.size() != 0){
			updateTime1 = "("+accountCountForCompanyHistorySeven.get(accountCountForCompanyHistorySeven.size()-1).getGdate() + " 00:28:00"  +")";
		}
		res.put("updateTime1",updateTime1);
		return res;
	}

	/**
	 * 定时刷业务资金备注BI数据
	 * @return
	 * @throws Exception
	 */
	@Override
	public R addByBusinessFundsRemark() throws Exception {
		biMapper.insertBiBusinessFundsRemarkList();
		return R.ok();
	}
}
